package com.ai.aichat.service.impl;

import com.ai.aichat.entity.Users;
import com.ai.aichat.mapper.UsersMapper;
import com.ai.aichat.service.IUsersService;
import com.ai.aichat.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.UUID;

/**
 * <p>
 * 用户基本信息表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2025-09-16
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
    @Autowired
    private UsersMapper usersMapper;

    @PostMapping("/register")
    public Result register(@RequestBody Users user){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        user.setUuid(UUID.randomUUID().toString());
        user.setCreatedAt(java.time.LocalDateTime.now());
        user.setUpdatedAt(java.time.LocalDateTime.now());
        user.setSubscriptionType("free");
        user.setStatus(true);
        user.setLoginCount(0);
        return Result.success(usersMapper.insert(user));
    }

    @Override
    public Object getByUsername(String username) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return usersMapper.selectOne(queryWrapper);
    }

//    @Override
//    public Object updatePassword(String password){
//        Users user = usersMapper.selectById()
//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        user.setPassword(encoder.encode(password);
//        return Result.success(usersMapper.update(user));
//    }
}
